<script lang="ts" setup>
import { FormRules } from 'element-plus'
import { cmsRequirementSolutionCreate } from '@/api/index'

import fileUpload from '@/components/fileUpload/index.vue'
import { areaJson } from '@/utils/area.ts'


interface DictItem {
  siteCode: string
  dictionaryCode: string
  dictionaryName: string
}

const emit = defineEmits(['refresh'])
const dictStore = useDictStore()

const formRef = ref()
const form = reactive({
  cmsRequirementReleaseId: '',
  schemeIntroduction: '',
  unitName: '',
  province: '',
  city: '',
  area: '',
  specificAddress: '',
  unitNature: '',
  unitSize: '',
  contacts: '',
  contactNumber: '',
  email: '',
  contactDepartment: '',
  contactTitle: '',
  releaseTime: '',
  attachment: '',
  attachmentName: '',
  status: '1'
})
const rules = reactive<FormRules>({
  schemeIntroduction: [{ required: true, message: '请输入方案简介', trigger: 'blur' }],
  unitName: [{ required: true, message: '请输入单位名称', trigger: 'blur' }],
  specificAddress: [{ required: true, message: '请选择单位地址', trigger: 'blur' }],
  unitNature: [{ required: true, message: '请选择单位性质', trigger: 'change' }],
  contacts: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
  contactNumber: [
    { required: true, message: '请输入联系电话', trigger: 'blur' },
    {
      pattern: /^1[3-9]\d{9}$/,
      message: '请输入有效的手机号码',
      trigger: 'blur'
    }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    {
      pattern:
        /^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/,
      message: '请输入有效的邮箱地址',
      trigger: 'blur'
    }
  ]
})

const show = ref(false)

const unitNatureOpt = computed(() => {
  return (dictStore.getDict as DictItem[])?.filter(
    (item: DictItem) => item.siteCode === 'JLY_UNITNATURE_CODE'
  )
})

const unitSizeOpt = computed(() => {
  return (dictStore.getDict as DictItem[])?.filter(
    (item: DictItem) => item.siteCode === 'JLY_UNITSIZE_CODE'
  )
})

let areaOpt: any[] = [...areaJson.data]
const areaProps = {
  value: 'xzqymc',
  label: 'xzqymc',
  children: 'child'
}
// 选择省市区
const areaChange = (val: any) => {
  form.province = val ? val[0] : ''
  form.city = val ? val[1] : ''
  form.area = val ? val[2] : ''
}

const open = (id: string) => {
  form.cmsRequirementReleaseId = id
  show.value = true
}
const close = () => {
  show.value = false
}
const submit = async () => {
  if (!formRef.value) return
  await formRef.value.validate((valid: any) => {
    if (valid) {
      form.releaseTime = getNowTime()
      cmsRequirementSolutionCreate({ data: form }).then(({ code }) => {
        if (code == 0) {
          ElMessage.success('投递方案成功')
          close()
          emit('refresh')
          // cmsRequirementReleaseProcess({ status: '2', id: form.cmsRequirementReleaseId }).then(
          //   ({ code }) => {
          //     if (code == 0) {
          //       ElMessage.success('投递方案成功')
          //       close()
          //       emit('refresh')
          //     }
          //   }
          // )
        }
      })
    }
  })
}

const getNowTime = () => {
  const now = new Date()
  const year = now.getFullYear()
  const month = now.getMonth() + 1
  const day = now.getDate()
  const hour = now.getHours()
  const minute = now.getMinutes()
  const second = now.getSeconds()
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`
}

// 获取文件名称
const fileChange = (data: any) => {
  form.attachmentName = data.name
}

// onMounted(() => {
//   fetch('/static/json/area.json')
//     .then((response) => response.json())
//     .then((res) => (areaOpt = [...res.data]))
// })

defineExpose({
  open,
  close
})
</script>
<template>
  <el-drawer class="drawer-wrap delivery-wrap" v-model="show">
    <template #header>
      <div class="drawer-title">方案提交</div>
    </template>
    <div class="drawer-content">
      <el-form ref="formRef" class="drawer-form" label-width="82px" :model="form" :rules="rules">
        <div class="title">方案信息</div>
        <el-form-item label="方案简介" prop="schemeIntroduction">
          <el-input
            class="drawer-item-width"
            :rows="3"
            type="textarea"
            maxlength="3000"
            placeholder="请输入3000字以内"
            v-model="form.schemeIntroduction"
          />
        </el-form-item>
        <el-form-item label="附件说明" prop="attachment">
          <div class="upload-box" style="align-items: center">
            <fileUpload class="upload" v-model="form.attachment" @fileChange="fileChange" />
            <span class="msg">仅支持doc/docx/pdf/jpg/jpeg/png/zip/rar格式，50M以内</span>
          </div>
        </el-form-item>
        <div class="title">单位基本信息</div>
        <el-form-item label="单位名称" prop="unitName">
          <el-input class="drawer-item-width" placeholder="请输入" v-model="form.unitName" />
        </el-form-item>
        <el-form-item label="单位地址" prop="specificAddress">
          <el-cascader
            class="drawer-item-unit"
            filterable
            clearable
            :options="areaOpt"
            :props="areaProps"
            @change="areaChange"
          />
          <el-input
            class="drawer-item-address"
            placeholder="请输入"
            v-model="form.specificAddress"
          />
        </el-form-item>
        <el-form-item label="单位性质" prop="unitNature">
          <el-select placeholder="请选择" class="drawer-item-width" v-model="form.unitNature">
            <el-option
              v-for="item in unitNatureOpt"
              :key="item.dictionaryCode"
              :label="item.dictionaryName"
              :value="item.dictionaryCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="企业规模" prop="unitSize">
          <el-select placeholder="请选择" class="drawer-item-width" v-model="form.unitSize">
            <el-option
              v-for="item in unitSizeOpt"
              :key="item.dictionaryCode"
              :label="item.dictionaryName"
              :value="item.dictionaryCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input class="drawer-item-width" placeholder="请输入" v-model="form.contacts" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactNumber">
          <el-input class="drawer-item-width" placeholder="请输入" v-model="form.contactNumber" />
        </el-form-item>
        <el-form-item label="邮箱地址" prop="email">
          <el-input class="drawer-item-width" placeholder="请输入" v-model="form.email" />
        </el-form-item>
        <el-form-item label="联系人部门" prop="contactDepartment">
          <el-input
            class="drawer-item-width"
            placeholder="请输入"
            v-model="form.contactDepartment"
          />
        </el-form-item>
        <el-form-item label="联系人职务" prop="contactTitle">
          <el-input class="drawer-item-width" placeholder="请输入" v-model="form.contactTitle" />
        </el-form-item>
        <!-- <el-form-item label="验证码">
          <el-input class="drawer-item-code" placeholder="请输入" />
          <div class="code">5588</div>
        </el-form-item> -->
      </el-form>
    </div>
    <template #footer>
      <el-button class="drawer-footer-btn" @click="close">取消</el-button>
      <el-button class="drawer-footer-btn" type="primary" @click="submit">提交</el-button>
    </template>
  </el-drawer>
</template>
<style lang="scss">
.delivery-wrap {
  width: 1180px !important;
}
.drawer-wrap {
  .el-drawer__header {
    padding: 0 34px;
    line-height: 40px;
    margin: 0;
    border-bottom: 1px solid #e9ebed;
    .drawer-title {
      font-size: 16px;
      color: #303133;
    }
  }
  .el-drawer__body {
    padding: 24px 34px;
    .drawer-content {
      .title {
        font-size: 16px;
        color: #303133;
        line-height: 22px;
        margin-bottom: 24px;
      }
      .drawer-form {
        .el-form-item {
          margin-bottom: 24px;
        }
        .el-form-item__label {
          justify-content: flex-start;
          margin-right: 22px;
        }
        .el-radio {
          margin-right: 16px;
        }
        .deadline {
          .el-form-item__label {
            line-height: 22px;
            height: auto;
          }
        }
        .drawer-item-width {
          width: 392px;
        }
        .drawer-item-budget {
          width: 333px;
          margin-right: 8px;
        }
        .budget {
          font-size: 14px;
          color: #000;
          line-height: 32px;
          width: 50px;
          text-align: center;
        }
        .drawer-item-requirementAreas {
          width: 485px;
        }
        .drawer-item-unit {
          width: 124px;
          margin-right: 8px;
        }
        .drawer-item-address {
          width: 260px;
        }
        .drawer-item-code {
          width: 268px;
          margin-right: 8px;
        }
        .code {
          width: 116px;
          height: 32px;
          background: #e9ebed;
          border-radius: 2px 2px 2px 2px;
          border: 1px solid #dcdfe6;
          padding: 0 13px;
          color: #a8abb2;
          line-height: 32px;
        }
        .upload-box {
          display: flex;
          align-items: flex-end;
          .upload {
            margin-right: 8px;
            .upload-customize {
              text-align: center;
              color: #909399;
              .text {
                line-height: 20px;
                font-size: 12px;
              }
            }
            // .el-upload--picture-card {
            //   width: 82px;
            //   height: 82px;
            // }
          }
        }

        .msg {
          font-size: 14px;
          color: #606266;
          line-height: 22px;
        }
      }
    }
  }
  .el-drawer__footer {
    padding: 16px 0;
    text-align: center;
    border-top: 1px solid #e9ebed;
    .drawer-footer-btn {
      width: 160px;
      height: 32px;
      border-radius: 0;
      &.el-button--primary {
        --el-button-bg-color: #1551e9;
        --el-button-hover-bg-color: #1551e9;
      }
    }
  }
}
</style>
